package models

import (
	"context"
	"log"

	"go.mongodb.org/mongo-driver/bson"
)

type UserBasic struct {
	UserId    string `bson:"user_id"`
	Account   string `bson:"account"`
	Password  string `bson:"password"`
	Nickname  string `bson:"nickname"`
	Icon      string `bson:"icon"`
	Sex       int    `bson:"sex"`
	LoginTime int    `bson:"login_time"`
	LoginIp   int    `bson:"login_ip"`
	Email     string `bson:"email"`
	Avatar    string `bson:"avatar"`
	CreatedAt int64  `bson:"create_at"`
	UpdateAt  int64  `bson:"update_at"`
}

func (UserBasic) CollectionName() string {
	return "user_basic"
}

func InsertOneUserBasic(ub *UserBasic) error {
	_, err := MongoDB.Collection(UserBasic{}.CollectionName()).
		InsertOne(context.Background(), ub)
	return err
}

func GetUserBasicByAccountPassword(account, password string) (*UserBasic, error) {
	ub := &UserBasic{}
	err := MongoDB.Collection(UserBasic{}.CollectionName()).
		FindOne(context.Background(), bson.D{
			{"account", account},
			{"password", password},
		}).Decode(ub)
	return ub, err
}

func GetUserBasicByIdentity(identity string) (*UserBasic, error) {
	ub := &UserBasic{}
	err := MongoDB.Collection(UserBasic{}.CollectionName()).
		FindOne(context.Background(), bson.D{
			{"user_id", identity},
		}).Decode(ub)
	return ub, err
}

func GetUserBasicByAccount(account string) (*UserBasic, error) {
	ub := &UserBasic{}
	err := MongoDB.Collection(UserBasic{}.CollectionName()).
		FindOne(context.Background(), bson.D{
			{"account", account},
		}).Decode(ub)
	return ub, err
}

func GetUserBasicCountByEmail(email string) (int64, error) {
	return MongoDB.Collection(UserBasic{}.CollectionName()).
		CountDocuments(context.Background(), bson.D{
			{"email", email},
		})
}

func GetUserBasicCountByAccount(account string) (int64, error) {
	return MongoDB.Collection(UserBasic{}.CollectionName()).
		CountDocuments(context.Background(), bson.D{
			{"account", account},
		})
}
func InsertRegiserMany(userBasicArray []interface{}) {

	collection := MongoDB.Collection(UserBasic{}.CollectionName())
	// 执行insertMany操作

	_, err := collection.InsertMany(context.TODO(), userBasicArray)
	if err != nil {
		log.Fatal(err)
	}
}
